package com.woniuxy.ticketorder.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.ticketorder.entity.TicketOrder;
import com.woniuxy.ticketorder.entity.dto.OrderDto;
import com.woniuxy.ticketorder.mapper.OrderMapper;
import com.woniuxy.ticketorder.service.OrderService;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.UUID;

/**
 * @ClassName OrderServiceImpl
 * @Description TODO
 * @Author zx
 * @Date 2021-02-27 17:42:01
 * @Version 1.0
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, TicketOrder> implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Override
    public Boolean addOrder(OrderDto orderDto) {
        //生成随机编号
        String uuid = UUID.randomUUID() + "";
        String substring = uuid.substring(uuid.lastIndexOf("-") + 1);
        //下单时间（生成）
        String ordertime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "";
        //订单号（生成）    发现生成过长再次截取大小于数据库一样
        String orderno = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date()) + substring.substring(0, 10);
        //用户（先写死）
        Integer userid = 1;
        //金额
        Double money = orderDto.getMoney();
        //支付时间（生成）
        String paytime = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
        //状态（是否支付）
        String status = "1";
        //影院
        String cinema = orderDto.getCinema();
        //影厅
        String hall = orderDto.getHall();
        //位置
        String seat = orderDto.getSeat();
        //切割一手数据库看起来可读性高一些
        String[] location = seat.split("\\,");
        int i = Integer.parseInt(location[0]);
        int y = Integer.parseInt(location[1]);
        seat = i + "排" + y + "号";
        //插入订单
        TicketOrder ticketOrder = new TicketOrder(orderno, ordertime, userid, money, paytime, cinema, hall, seat, status);
        return orderMapper.insert(ticketOrder) > 0 ? true : false;
    }
}
